package test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import lights.adapters.Field;
import lights.dracots.Tuple;
import lights.interfaces.ITuple;
import lime2.LimeServerID;
import lime2.Location;
import lime2.TupleID;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.ShareTuple;
import sagroup.dracots.TupleType;

public class OutInpAgent implements Runnable {
	@Override
	public void run() {
		// TODO Auto-generated method stub
		DracoTS dts = DracoTS.get("testing");

		for (int i = 1; i < 10; i++) {
			ITuple it = new Tuple();
			it.add(new Field().setValue("Hello" + i));
			DracoTuple ret = dts.out(new FlatShareTuple(new TupleType("type"),
					it));
			if (ret != null) {
				System.out.println("OutInpAgent has out tuple with vid: "
						+ ret.getVID());
			}
		}
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Then delete the tuples
		LimeServerID localID = null;
		try {
			localID = new LimeServerID(InetAddress.getLocalHost(), 30000);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for(int i = 1; i < 10; i++){
			TupleID vid = new TupleID(localID, i);
			ShareTuple template = ShareTuple.createVIDTemplate(vid);
			template.setCurr(new Location(localID));
			template.setReplica(false);
			DracoTuple ret = dts.inp(template);
			if (ret != null) {
				System.out.println("OutInpAgent has read and delete tuple with vid: "
						+ ret.getVID());
			}
		}
	}
}
